package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleClient;
import com.heima.user.feign.MediaClient;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-06-21
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Autowired
    private IApUserService userService;
    /**
     * 用户认证列表分页查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listStatus(AuthDto dto) {
        //判断前端传的状态是否为空，为空查询全部，不为空添加查询条件
        //构建查询条件
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        if (dto.getStatus() != null){
            queryWrapper.eq(ApUserRealname::getStatus,dto.getStatus());
        }
        //构建分页条件
        Page<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        Page<ApUserRealname> page1 = this.page(page, queryWrapper);
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), page1.getTotal(), page1.getRecords());
        return result;



    }

    /**
     * 审核
     * @param dto
     * @param type 1成功 0失败
     * @return
     */
    @Override
    public ResponseResult auth(AuthDto dto, int type) {
        //根据id查询认证表的数据
        ApUserRealname userRealname = this.getById(dto.getId());
        //失败，修改用户认证状态为2 保存失败的原因
        if (type == 0){
            userRealname.setStatus(2);
            userRealname.setReason(dto.getReason());
            this.updateById(userRealname);
            return ResponseResult.okResult();
        }
        //审核成功
        //创建自媒体账号
        //查询ap_user
        ApUser apUser = userService.getById(userRealname.getUserId());
        //创建自媒体账号 注册信息从ap_user同步
        WmUser wmUser = createWmUser(apUser);
        //创建作者账号
        if (wmUser!=null){
            ApAuthor apAuthor = createAuthor(apUser,wmUser);
            if (apAuthor != null){
                //认证表的状态修改为9 审核通过
                userRealname.setStatus(9);
                this.updateById(userRealname);
                //修改用户注册表的flag 为1 自媒体人
                apUser.setFlag(1);
                userService.updateById(apUser);
            }
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    /**
     * 创建文章作者
     * @param apUser
     * @param wmUser
     * @return
     */
    private ApAuthor createAuthor(ApUser apUser, WmUser wmUser) {
        //构建文章作者
        ApAuthor author = new ApAuthor();
        author.setName(apUser.getName());
        author.setType(2);
        author.setUserId(apUser.getId());
        author.setWmUserId(wmUser.getId());
        //远程调用文章服务接口
        ResponseResult<ApAuthor> responseResult = articleClient.saveApAuthor(author);
        //判断操作成功
        if (responseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())){
            //返回作者数据
            ApAuthor apAuthor = responseResult.getData();
            //作者的id写回到自媒体用户表中
            wmUser.setApAuthorId(apAuthor.getId());
            mediaClient.updateUser(wmUser);
            return apAuthor;
        }
        return null;
    }

    @Autowired
    private MediaClient mediaClient;

    @Autowired
    private ArticleClient articleClient;

    /**
     * 创建自媒体用户
     * @param apUser
     * @return
     */
    private WmUser createWmUser(ApUser apUser) {
        //构建自媒体用户
        WmUser wmuser = new WmUser();
        //基本属性从apUser复制
        BeanUtils.copyProperties(apUser,wmuser);
        wmuser.setId(null);
        wmuser.setApUserId(apUser.getId());
        wmuser.setName(apUser.getName());
        wmuser.setStatus(9);
        wmuser.setType(0);
        //远程调用自媒体media接口
        ResponseResult<WmUser> responseResult = mediaClient.saveUser(wmuser);
        //判断调用的结果
        if (responseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())){
            WmUser user = responseResult.getData();
            return user;
        }
        return null;
    }


}
